package;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.input.gamepad.FlxGamepad;
import flixel.input.keyboard.FlxKey;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import flixel.input.gamepad.LogitechButtonID;
import flixel.input.gamepad.PS4ButtonID;

/**
 * ...
 * @author ...
 */
class PlayerInput extends FlxObject
{
	private var up:String;
	private var down:String;
	private var left:String;
	private var right:String;
	private var chosen:Bool;
	private var choice:Int;
	public var gamePad: FlxGamepad;
	private var type:Int;
	public function new(up:String,down:String,left:String,right:String,gamepad:FlxGamepad) 
	{
		super();
		this.gamePad = gamepad;
		this.up = up;
		this.down = down;
		this.left = left;
		this.right = right;
		chosen = false;
		choice = -1;
	}

	public function getInput():Int
	{
		return choice;
	}
	
	public function setInput(input:Int):Void
	{
		choice = input;
	}
	
	override function update():Void
	{
		if (!chosen)
		{
			if (gamePad == null) 
			{
				if (FlxG.keys.anyJustPressed([up]))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 1;
					chosen = true;
				}
				if (FlxG.keys.anyJustPressed([down]))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 3;
					chosen = true;
				}
				if (FlxG.keys.anyJustPressed([left]))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 2;
					chosen = true;
				}
				if (FlxG.keys.anyJustPressed([right]))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 4;
					chosen = true;
				}		
			}
			else
			{
				if (gamePad != null && gamePad.justPressed(LogitechButtonID.ONE))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 1;
					chosen = true;
					trace("ONE");
				}
				if (gamePad != null && gamePad.justPressed(LogitechButtonID.THREE))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 3;
					chosen = true;
					trace("THREE");
				}
				if (gamePad != null && gamePad.justPressed(LogitechButtonID.FOUR))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 2;
					chosen = true;
					trace("FOUR");
				}
				if (gamePad != null && gamePad.justPressed(LogitechButtonID.TWO))
				{
					FlxG.sound.play("assets/sounds/OpcionElegida.wav");
					choice = 4;
					chosen = true;
					trace("TWO");
				}	
			}
		}
	}
}